package com.wudl.ums.controller;


import com.wudl.ums.entity.OmsOrder;
import com.wudl.ums.service.OmsOrderService;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.wudl.ums.utils.CommonResult;
import com.wudl.ums.utils.PageResult;
import com.wudl.ums.vo.OmsOrderVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单表 Service
 *
 * @author wudl
 */
@Api
@RestController
@RequestMapping("/omsOrder")
public class OmsOrderController {

    @Autowired
    private OmsOrderService omsOrderService;

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public PageResult<OmsOrderVO> page(@RequestBody OmsOrderVO pageVO, @RequestParam Integer page, @RequestParam Integer size) {
        PageInfo<OmsOrder> pageInfo = omsOrderService.page(page, size);
        // 拷贝结果
        List<OmsOrderVO> list = pageInfo.getList().stream().map(it -> BeanUtil.copyProperties(it, OmsOrderVO.class)).collect(Collectors.toList());
        return PageResult.of(pageInfo.getTotal(), list);
    }

    /**
     * 查询详情
     */
    @GetMapping("/get/{id}")
    public CommonResult<OmsOrderVO> get(@PathVariable Integer id) {
        OmsOrder entity = omsOrderService.getById(id);
        return CommonResult.success(BeanUtil.copyProperties(entity, OmsOrderVO.class));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public CommonResult<Void> add(@RequestBody OmsOrderVO addVO) {
        omsOrderService.save(BeanUtil.copyProperties(addVO, OmsOrder.class));
        return CommonResult.success();
    }

    /**
     * 编辑
     */
    @PutMapping("/edit")
    public CommonResult<Void> edit(@RequestBody OmsOrderVO editVO) {
        omsOrderService.updateById(BeanUtil.copyProperties(editVO, OmsOrder.class));
        return CommonResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public CommonResult<Void> delete(@PathVariable Integer id) {
        omsOrderService.removeById(id);
        return CommonResult.success();
    }

}
